﻿using System;
using UnityEngine;
using System.Collections;
using System.IO;
using ICSharpCode.SharpZipLib.Zip;
using Inlycat.Log;

public class ZipTools
{
    public static void UnZip(string orig, string dest, Action onFinished)
    {
        if (!File.Exists(orig))
        {
            return;
        }

        using (ZipInputStream s = new ZipInputStream(File.OpenRead(orig)))
        {

            ZipEntry theEntry;
            while ((theEntry = s.GetNextEntry()) != null)
            {

                PLog.Log(theEntry.Name);

                string newFilePath = Path.Combine(dest, theEntry.Name);
                string directoryName = Path.GetDirectoryName(newFilePath);
                string fileName = Path.GetFileName(newFilePath);

                // create directory
                if (directoryName.Length > 0)
                {
                    Directory.CreateDirectory(directoryName);
                }

                if (fileName != String.Empty)
                {
                    using (FileStream streamWriter = File.Create(newFilePath))
                    {

                        int size = 2048;
                        byte[] data = new byte[2048];
                        while (true)
                        {
                            size = s.Read(data, 0, data.Length);
                            if (size > 0)
                            {
                                streamWriter.Write(data, 0, size);
                            }
                            else
                            {
                                break;
                            }
                        }
                    }
                }
            }

            // delete the original package
            File.Delete(orig);
            onFinished();
        }
    }


    public static IEnumerator UnZip(string orig, string dest, Action onFinished, Action<string, float> onReportProcessPercent)
    {
        if (!File.Exists(orig))
        {
            yield break;
        }
        long fileSize = new FileInfo(orig).Length;
        using (ZipInputStream s = new ZipInputStream(File.OpenRead(orig)))
        {
            ZipEntry theEntry;
            while ((theEntry = s.GetNextEntry()) != null)
            {
                yield return null;

                string newFilePath = Path.Combine(dest, theEntry.Name);
                string directoryName = Path.GetDirectoryName(newFilePath);
                string fileName = Path.GetFileName(newFilePath);

                // create directory
                if (directoryName.Length > 0)
                {
                    Directory.CreateDirectory(directoryName);
                }

                if (fileName != String.Empty)
                {
                    using (FileStream streamWriter = File.Create(newFilePath))
                    { 
                        int size = 2048;
                        byte[] data = new byte[2048];
                        while (true)
                        {
                            size = s.Read(data, 0, data.Length);
                            if (size > 0)
                            {
                                streamWriter.Write(data, 0, size);
                            }
                            else
                            {
                                break;
                            }
                            onReportProcessPercent("正在解压：" + theEntry.Name, s.Position / (float)fileSize);
                            yield return null;
                        }
                    }
                }
            }

            onFinished();

        }
        // delete the original package
        File.Delete(orig);
    }
}
